package com.zfk.framework.case_loader;

import com.zfk.framework.casefield_validator.CaseValidator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.zfk.framework.models.CaseData;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AllTestCaseLoader {
    public static final Logger logger = LogManager.getLogger(AllTestCaseLoader.class);

    public static Map<String,CaseData> loaderTestCase(List<String>yamlFileList)throws IOException {
        logger.info("开始加载用例,文件数量{}",yamlFileList.size());

        //局部变量，仅在函数内部有效，作为返回值 需要静态变量接收
        Map<String,CaseData> casePool = new HashMap<>();

        //遍历yaml文件名列表，挨个打开，校验 然后加载
        for(String yamlFilePath:yamlFileList){
            Map<String,Map<String,Object>> yamlCaseMap = YamlCaseLoader.readYamlCase(yamlFilePath);
            //yamlCaseMap是单个yaml中的所有测试用例，需要遍历其键值对,去拿到单个测试用例
            for(Map.Entry<String,Map<String,Object>> entry:yamlCaseMap.entrySet()){
                //caseId一般会以接口名称+数字命名，这里暂时不对caseId的唯一性进行校验
                //这里entry就表示一条完整的测试用例
                Map<String,Object> caseDataMap = entry.getValue();
                CaseData caseDataObj= CaseValidator.validateAndParseCase(
                        entry.getKey(),
                        caseDataMap,
                        yamlFilePath
                );
                //校验通过往用例池中添加
                casePool.put(entry.getKey(),caseDataObj);
                logger.info("用例{}加载成功",entry.getKey());
            }
        }
        logger.info("用例加载完毕，用例数量{}",casePool.size());
        return casePool;
    }

}
